package jp.co.realsys.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import jp.co.realsys.error.TaskException;
import jp.co.realsys.model.StudentModel;
import jp.co.realsys.service.StudentService;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.validator.annotations.VisitorFieldValidator;

@Controller
@Scope("prototype")
public class StudentAction extends ActionSupport implements Preparable {
	private static final long serialVersionUID = 1L;

	@Autowired
	private StudentService studentService;

	// List of student; Setter and Getter are below
	private List<StudentModel> students;
    
	// Student object to be added; Setter and Getter are below
	private StudentModel student;
	
	HttpServletRequest request=ServletActionContext.getRequest();  
	HttpSession session=request.getSession();
	String s=(String)session.getAttribute("login");
	
	public String menu() {
		if(s!=null && !s.equals("")){
			student = null;
			return SUCCESS;
		}
		else{  
		    return ERROR;  
		} 
	}

	// This method return list of students in database
	@SkipValidation
	public String studentSearch() throws Exception {
		if(s!=null && !s.equals("")){
			if (student == null || student.getName() == null
					|| student.getName().length() == 0) {
				students = studentService.getAllStudents();
			} else {
				students = studentService.doSelectStduentList(student.getName());
			}
			if (students.size() == 0){
				throw new TaskException("該当学生の情報が存在しません");
			}
			return SUCCESS;
		}else {
			return ERROR;
		}
		
	}
	
	public String studentRegister() throws TaskException {
		if(s!=null && !s.equals("")){
			if (student == null) {
				students = studentService.getAllStudents();
				return INPUT;
			}
			if (studentService.getStudentById(student.getId()) != null){
				throw new TaskException("該当学生は登録している記録がありました、再登録ことができません。");
			}else {
				int result = studentService.doAddStduent(student);
				ActionContext ctx = ActionContext.getContext();
				String message = "";
				if(result > 0) {
					message = "該当学生の情報を登録成功しました。";
				}else {
					message ="該当学生は登録している記録がありました、再登録ことができません。";
				}
				ctx.put("req", message);
				//登録されった学生情報を表示する
				students = new ArrayList<StudentModel>();
				students.add(studentService.getStudentById(student.getId()));
			}
			return SUCCESS;
		}else{
			return ERROR;
		}
	}

	@SkipValidation
	public String studentDelete() throws TaskException {
		if(s!=null && !s.equals("")){
			if (student == null) {
				students = studentService.getAllStudents();
				return INPUT;
			}
			if (StringUtils.isEmpty(student.getId())) {
				super.addFieldError("id", super.getText("requiredstring2", "", new String [] { getText("id")}));
				return INPUT;
			}
			int result = studentService.doDeleteStduent(student.getId());
			ActionContext ctx = ActionContext.getContext();
			String message = "";
			if(result > 0) {
				message = "該当学生の情報を削除成功しました。";
			}else {
				message ="該当学生の情報の情報が存在しません、削除失敗しました。";
				throw new TaskException(message);
			}
			ctx.put("req", message);
			students = studentService.getAllStudents();
			return SUCCESS;
		}else{
			return ERROR;
		}
		
	}
	public String studentUpdate() throws TaskException {
		if(s!=null && !s.equals("")){
			if (student == null) {
				students = studentService.getAllStudents();
				return INPUT;
			}
			int result = studentService.doUpdateStudent(student);
			ActionContext ctx = ActionContext.getContext();
			String message = "";
			if(result > 0) {
				message = "該当学生の情報を更新成功しました。";
			}else {
				message = "更新失敗しました、該当学生の情報が存在しません";
				throw new TaskException(message);
			}
			ctx.put("req", message);
			students = new ArrayList<StudentModel>();
			students.add(studentService.getStudentById(student.getId()));
			return SUCCESS;
		}else{
			return ERROR;
		}
	}
	// This method will be called before any of Action method is invoked;
	// So some pre-processing if required.
	@Override
	public void prepare() throws Exception {
	}

	// Getters and Setters hidden

	public List<StudentModel> getStudents() {
		return students;
	}

	public void setStudents(List<StudentModel> students) {
		this.students = students;
	}

	@VisitorFieldValidator(message="")
	public StudentModel getStudent() {
		return student;
	}

	public void setStudent(StudentModel student) {
		this.student = student;
	}
	
}